import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { FormInstance } from 'antd/lib/form';
import { Modal, Button, Form, message, Input, DatePicker, Select, InputNumber } from 'antd';
import PropTypes from 'prop-types';
import moment from 'moment';
import { getDictOptionsByCode } from '@/support/common';

const FormItem = Form.Item;
const mapStateToProps = (state: any) => ({
  dataFieldManagement: state.dataFieldManagement,
});
class _AddModal extends PureComponent<any, any> {
  formRef = React.createRef<FormInstance>();
  static contextTypes = {
    permissions: PropTypes.array,
    dicData: PropTypes.array,
  };

  constructor(props: any) {
    super(props);
    this.state = {
      loading: false,
    };
  }

  componentDidUpdate(prevProps: any) {
    const { modalVisible, formData } = this.props;
    if (modalVisible && modalVisible !== prevProps.modalVisible) {
      this.formRef &&
        this.formRef.current?.setFieldsValue({
          ...formData,
        });
    }
  }

  handleSubmit = () => {
    this.formRef.current?.submit();
  };

  onFinish = (values: any) => {
    const { dispatch, formData, freshPage } = this.props;
    let postData = {
      ...values,
    };
    let dType = 'dataFieldManagement/postObjWithRes';
    let url = '/datafield/add'; //新增
    if (formData?.id) {
      postData.id = formData?.id;
      // dType = 'dataFieldManagement/putObjWithRes';
      url = `/datafield/update`; //编辑
    }
    this.setState({
      loading: true,
    });
    dispatch({
      type: dType,
      payload: {
        url: url,
        ...postData,
      },
      callback: (res: any) => {
        if (res.statusCode === '0') {
          message.success(postData.id ? '编辑成功' : '新增成功');
          this.close(postData.id ? 'edit' : 'add');
        }
      },
    }).finally(() => {
      this.setState({
        loading: false,
      });
    });
  };

  close = (refresh = false as string | boolean) => {
    const { closeModal } = this.props;
    this.formRef.current && this.formRef.current.resetFields();
    closeModal(refresh);
  };

  render() {
    const { formData, modalVisible } = this.props;
    const { loading } = this.state;
    const footer = (
      <div>
        <Button size="small" onClick={() => this.close()}>
          取消
        </Button>
        <Button size="small" loading={loading} onClick={this.handleSubmit} type="primary">
          确定
        </Button>
      </div>
    );
    let title = formData.id ? '编辑字段' : '新增字段';

    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: {
          span: 6,
          // offset: 4,
        },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: {
          span: 14,
        },
        md: { span: 16 },
      },
    };

    return (
      <Modal
        title={title}
        footer={footer}
        visible={modalVisible}
        width={700}
        onCancel={() => this.close()}
      >
        <Form style={{ marginTop: 12 }} ref={this.formRef} onFinish={this.onFinish}>
          <FormItem
            {...formItemLayout}
            label="数据对象"
            colon={false}
            name="dataObj"
            rules={[
              {
                required: true,
                whitespace: true,
                message: '请输入数据对象(限制字母、数字、下划线_、方括号[]和小数点.)',
                pattern: /^[\w\.\_\[\]]+$/,
              },
            ]}
          >
            <Input placeholder="请输入数据对象" maxLength={100} />
          </FormItem>
          <FormItem
            {...formItemLayout}
            label="数据类别"
            colon={false}
            name="dataCag"
            // rules={[{required: false, message: '请输入车牌号，只能输入中英文、数字及特殊字符', pattern: /^[\u4e00-\u9fa5a-zA-Z0-9\s\w]+$/,}]}
            rules={[{ required: true, whitespace: true, message: '请输入数据类别' }]}
          >
            <Input placeholder="请输入数据类别" maxLength={50} />
          </FormItem>
          <FormItem
            {...formItemLayout}
            label="字段名称"
            colon={false}
            name="fieldName"
            // rules={[{required: false, message: '请输入车牌号，只能输入中英文、数字及特殊字符', pattern: /^[\u4e00-\u9fa5a-zA-Z0-9\s\w]+$/,}]}
            rules={[{ required: true, whitespace: true, message: '请输入字段名称' }]}
          >
            <Input placeholder="请输入字段名称" maxLength={50} />
          </FormItem>
        </Form>
      </Modal>
    );
  }
}
export const AddModal = connect(mapStateToProps)(_AddModal);
